package com.amoyhong.hongWhether.database;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

/**
 * Created by Administrator on 2016/1/17.
 */
@Table(name = "cities")
public class City extends Model {

    @Column(name = "name")
    public String name;

    @Column(name = "latitude")
    public float latitude;

    @Column(name = "longitude")
    public float longitude;

    public static class CitiesDeserializer implements JsonDeserializer<City> {
        private static final String TAG = "CitiesDeserializer";
        @Override
        public City deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

            City city = new City();

            final JsonObject jsonObject = json.getAsJsonObject();

            city.name = jsonObject.get("name").getAsString();
            city.latitude = jsonObject.get("latitude").getAsFloat();
            city.longitude = jsonObject.get("longitude").getAsFloat();

            return city;
        }
    }

    public static void deleteAll(){
        new Delete().from(City.class).execute();
    }

    @Override
    public String toString() {
        return "City{" +
                "latitude=" + latitude +
                ", name='" + name + '\'' +
                ", longitude=" + longitude +
                '}';
    }
}
